﻿using System.Collections.Generic;
using ML.Entity;
using ML.IBusiness;
using ML.IRepository;
using System.Linq;

namespace ML.Business
{
    /// <summary>
    /// 产品管理业务类
    /// </summary>
    public class ProductBusiness : IProductBusiness
    {
        private readonly IProductRepository productRepository;
        private readonly IOrderDetailsRepository orderDetailsRepository;
        public ProductBusiness(IProductRepository _productRepository, IOrderDetailsRepository _orderDetailsRepository)
        {
            productRepository = _productRepository;
            orderDetailsRepository = _orderDetailsRepository;
        }

        public BaseResult<Pager<ProductResponse>> List(ProductSearch productSearch)
        {
            Pager<ProductResponse> data = productRepository.List(productSearch);
            return new BaseResult<Pager<ProductResponse>>(200, data);
        }

        public BaseResult<IEnumerable<ProductResponse>> GetListByMerchantId(string id, int pageindex, int pagesize)
        {
            var data = productRepository.GetListByMerchantId(id, pageindex, pagesize);
            if (data.Count() <= 0)
            {
                return new BaseResult<IEnumerable<ProductResponse>>(202);
            }
            return new BaseResult<IEnumerable<ProductResponse>>(200, data);
        }

        public BaseResult<ProductResponse> GetDetailById(string id)
        {
            var data = productRepository.GetDetailById(id);
            return new BaseResult<ProductResponse>(200, data);
        }

        public BaseResult<bool> Add(ProductEntity productEntity)
        {
            var isTrue = productRepository.Add(productEntity);
            if (isTrue)
            {
                return new BaseResult<bool>(200, true);
            }
            return new BaseResult<bool>(201, false);
        }

        public BaseResult<bool> Update(ProductEntity productEntity)
        {
            var isTrue = productRepository.Update(productEntity);
            if (isTrue)
            {
                return new BaseResult<bool>(200, true);
            }
            return new BaseResult<bool>(201, false);
        }

        public BaseResult<bool> UalMark(string id, int ualmark)
        {
            var isTrue = productRepository.UalMark(id, ualmark);
            if (isTrue)
            {
                return new BaseResult<bool>(200, true);
            }
            return new BaseResult<bool>(201, false);
        }

        public BaseResult<bool> Delete(string id)
        {
            //首先判断该产品是否已经含有订单，如果含有订单则不允许删除
            var data = orderDetailsRepository.GetOrderDetailByProductId(id);
            if (data.Count() > 0)
            {
                return new BaseResult<bool>(1080);
            }

            var isTrue = productRepository.Delete(id);
            if (isTrue)
            {
                return new BaseResult<bool>(200, true);
            }
            return new BaseResult<bool>(201, false);
        }
    }
}